﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using GQ.Controls.Tools;
using Chili.Opf3;
using GQ.Utils;
using GQ.BLL.QuanLyKho;
using GQ.WinForms;
namespace GQ.QuanLyKho
{
    public partial class frmListChiNhanh : frmBase
    {
        public frmListChiNhanh()
        {
            InitializeComponent();
            GridUtils.SetStyle(gridView1);
        }

        private void frmListChiNhanh_Load(object sender, EventArgs e)
        {
            gridControl1.DataSource = ChiNhanh.LoadAllBy("CompanyID={0}", Config.CompanyID);
        }

        private void menuButton1_AddNewClick()
        {
            frmChiNhanh frm = new frmChiNhanh();
            frm.ChiNhanh = new ChiNhanh()
            {
                CompanyID = new Guid(Config.CompanyID)
            };
            try
            {
                if (frm.ShowDialog(this) == DialogResult.OK)
                {
                    IList<ChiNhanh> chinhanhs = (IList<ChiNhanh>)gridControl1.DataSource;
                    chinhanhs.Add(frm.ChiNhanh);
                    Helpers.Project.SetStatus("Lưu thành công", StatusInfo.Complete);
                }
            }
            catch (Exception ex)
            {
                Helpers.Log(ex);
                Helpers.Project.SetStatus("Lưu không thành công", StatusInfo.Error);

            }
            menuButton1.Action = GQ.Controls.ActionState.View;
        }

        private void menuButton1_DeleteClick()
        {
            try
            {
                ChiNhanh chinhanh = (ChiNhanh)gridView1.GetFocusedRow();
                if (chinhanh == null)
                {
                    return;
                }
                if (Box.Show("Bạn có muốn xóa chi nhánh này không?", MessageBoxButtons.YesNo, MessageBoxIcon.Question) != DialogResult.Yes)
                    return;
                chinhanh.Delete();
                IList<ChiNhanh> chinhanhs = (IList<ChiNhanh>)gridControl1.DataSource;
                chinhanhs.Remove(chinhanh);
                Helpers.Project.SetStatus("Xóa thành công", StatusInfo.Complete);
            }
            catch (Exception ex)
            {
                Helpers.Log(ex);
                Helpers.Project.SetStatus("Xóa không thành công", StatusInfo.Error);

            }
        }

        private void menuButton1_EditClick()
        {
            ChiNhanh chinhanh = (ChiNhanh)gridView1.GetFocusedRow();
            if (chinhanh == null)
            {
                menuButton1.Action = GQ.Controls.ActionState.View;
                return;
            }
            try
            {
                frmChiNhanh frm = new frmChiNhanh();
                frm.ChiNhanh = chinhanh;
                if (frm.ShowDialog(this) == DialogResult.OK)
                    Helpers.Project.SetStatus("Lưu thành công", StatusInfo.Complete);
            }
            catch (Exception ex)
            {
                Helpers.Log(ex);
                Helpers.Project.SetStatus("Lưu không thành công", StatusInfo.Error);

            }
            menuButton1.Action = GQ.Controls.ActionState.View;
        }

        private void gridControl1_DoubleClick(object sender, EventArgs e)
        {
            menuButton1_EditClick();
        }
    }
}
